﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace RTS
{
	public abstract class RTSTextures
	{
		protected Dictionary<string, List<Texture2D>> loadedTextures = new Dictionary<string, List<Texture2D>>();
		protected string selectionArea;
		protected string lifeBar;
		protected string lifeBarBackground;
		protected string selected;

		protected Texture2D selectedTexture;
		protected Texture2D selectionAreaTexture;
		protected Texture2D lifeBarTexture;
		protected Texture2D lifeBarBackgroundTexture;

		protected float lifeBarOffsetX = 137;
		protected float lifeBarWidth = 50;
		protected TextureSize textureSize;
		protected bool texturesLoaded = false;

		public string Seleted
		{
			set { selected = value; }
		}

		public string LifeBar
		{
			set { lifeBar = value; }
		}

		public string LifeBarBackground
		{
			set { lifeBarBackground = value; }
		}

		public string SelectionArea
		{
			set { selectionArea = value; }
		}

		public float LifeBarOffsetX
		{
			set { lifeBarOffsetX = value; }
			get { return lifeBarOffsetX; }
		}

		public float LifeBarWidth
		{
			set { lifeBarWidth = value; }
			get { return lifeBarWidth; }
		}

		public TextureSize TextureSize
		{
			get { return textureSize; }
		}

		public RTSTextures(TextureSize textureSize)
		{
			this.textureSize = textureSize;
		}

		public virtual void LoadContent(ContentManager Content)
		{
			selectionAreaTexture = Content.Load<Texture2D>(selectionArea);
			selectedTexture = Content.Load<Texture2D>(selected);
			lifeBarTexture = Content.Load<Texture2D>(lifeBar);
			lifeBarBackgroundTexture = Content.Load<Texture2D>(lifeBarBackground);

			texturesLoaded = true;
		}

		public List<Texture2D> GetTexture(string state)
		{
			if (texturesLoaded == false) Trace.WriteLine("Error GetTexture RTSTextures");

			return loadedTextures[state];
		}

		public Texture2D GetSelectionAreaTexture()
		{
			if (texturesLoaded == false) Trace.WriteLine("Error GetSelectionAreaTexture RTSTextures");

			return selectionAreaTexture;
		}

		public Texture2D GetSelectedTexture()
		{
			if (texturesLoaded == false) Trace.WriteLine("Error getSelectedUnitTexture RTSTextures");

			return selectedTexture;
		}

		public Texture2D GetLifeBarTexture()
		{
			if (texturesLoaded == false) Trace.WriteLine("Error GetLifeBarTexture RTSTextures");

			return lifeBarTexture;
		}

		public Texture2D GetLifeBarBackgroundTexture()
		{
			if (texturesLoaded == false) Trace.WriteLine("Error GetLifeBarBackgroundTexture RTSTextures");

			return lifeBarBackgroundTexture;
		}

		protected void LoadTexture(string state, string textureName, ContentManager Content)
		{
			if (TextureNameExist(textureName + "_" + "1", Content) == false)
			{
				LoadSimpleTexture(state, textureName, Content);
			}
			else
			{
				int i = 1;

				while (TextureNameExist(textureName + "_" + i.ToString(), Content))
				{
					LoadSimpleTexture(state, textureName + "_" + i.ToString(), Content);
					i++;
				}

			}

		}

		protected void LoadSimpleTexture(string state, string textureName, ContentManager Content)
		{
			if (loadedTextures.ContainsKey(state) == false) loadedTextures[state] = new List<Texture2D>();

			loadedTextures[state].Add(Content.Load<Texture2D>(textureName));
		}

		protected bool TextureNameExist(string textureName, ContentManager Content)
		{
			try
			{
				Content.Load<Texture2D>(textureName);
				return true;
			} catch (Exception) {
				return false;
			}


			/*if (File.Exists("C:\\Users\\Fede\\Documents\\Visual Studio 2010\\Projects\\RTS\\RTS\\RTSContent\\" + textureName + ".png"))
				return true;

			return false;*/
		}


	}
}
